﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;

namespace LibreTimeTracker
{
  class SQLHelper
  {
    private static string _connString;

    public static string GlobalConnectionString
    {
      get {

        if (_connString == null)
        {
          _connString = ConfigurationManager.ConnectionStrings["TTConnectionString"].ConnectionString;
        }

        return _connString;

      }
    }

    public static SqlDataReader execSTP(string name, Dictionary<string, object> args)
    {
      if (String.IsNullOrEmpty(GlobalConnectionString)) throw new SettingsPropertyNotFoundException();

      SqlDataReader sdr = null;

      if (!string.IsNullOrEmpty(name))
      {
        SqlConnection conn = new SqlConnection(GlobalConnectionString);
        conn.Open();
        SqlCommand sc = conn.CreateCommand();
        sc.CommandType = System.Data.CommandType.StoredProcedure;
        sc.CommandText = name;

        if (args != null)
        {
          foreach (KeyValuePair<string, object> item in args)
          {
            sc.Parameters.Add(new SqlParameter(item.Key, item.Value));
          }
        }

        sdr = sc.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
      }

      return sdr;
    }
  }
}
